import { defineConfig } from 'vite'
import vue from '@vitejs/plugin-vue'
import dts from 'vite-plugin-dts'
import { fileURLToPath } from 'node:url'
import { dirname, resolve } from 'node:path'

const __filename = fileURLToPath(import.meta.url)
const __dirname = dirname(__filename)

export default defineConfig({
	plugins: [
		vue(),
		dts({
			entryRoot: 'src',
			insertTypesEntry: true,
			rollupTypes: true,
			outputDir: 'dist',
			include: ['src'],
		})
	],
	build: {
		lib: {
			entry: resolve(__dirname, 'src/index.ts'),
			name: 'TextFit',
			formats: ['es', 'cjs'],
			fileName: (format: string) => format === 'es' ? 'textfit.es.js' : 'textfit.cjs.js',
		},
		rollupOptions: {
			input: {
				core: resolve(__dirname, 'src/index.ts'),
				vue: resolve(__dirname, 'src/vue/index.ts'),
			},
			output: [
				{
					dir: 'dist',
					entryFileNames: (chunk: any) => chunk.name === 'vue' ? 'vue/index.es.js' : 'textfit.es.js',
					format: 'es',
					sourcemap: true,
				},
				{
					dir: 'dist',
					entryFileNames: (chunk: any) => chunk.name === 'vue' ? 'vue/index.cjs.js' : 'textfit.cjs.js',
					format: 'cjs',
					sourcemap: true,
				}
			],
			external: ['vue']
		}
	}
}) 